
    let cont = $('.sp')
    let end = $('.end')[0]
    let data = JSON.parse(localStorage.getItem('cart')) || []
    // 商品渲染
    function shopping() {
        cont[0].innerHTML = ''
        if (!data.length) {
            cont[0].innerHTML = `
                    <div class='null'>
                        <a href="../index.html">今日推荐</a>
                    </div>
                    `
        } else {
            end.style.display = 'block'
            cont[0].className = 'sp'
            data.forEach(item => {
                cont[0].innerHTML += `
                    <div class="shop">
                    <div class="hear">
                     <input type="checkbox" ${item.is_select && "checked"} >
                     <img src="" alt="">
                        <p>商家</p>
                         <a href="#"></a>
                    </div>
                    <div class="main">
                    <input type="checkbox" value="" data-id="${item.goods_id}" ${item.is_select && "checked"} class="select">
                    <img src="${item.img_big_logo}" alt="">
                    <div class="gauge">
                    <div class="box-1"><span></span><i></i></div>
                    <div class="box-2">
                    <div class="l">
                     商品名
                     </div>
                     <div class="l-two"><span>￥${item.current_price}</span>/<i>￥${item.price}</i></div>
                    <form class="l-c">
                        <input type="button" class="del" value="-" data-id="${item.goods_id}">
                        <input type="text" value="${item.cart_number}" class="val">
                         <input type="button" class="add" value="+" data-id="${item.goods_id}">
                    </form>
                        <div class="price">￥${(item.current_price * item.cart_number).toFixed(2)}</div>
                         <div class="dell" data-id="${item.goods_id}">删除</div>
                    </div>
                </div>
            </div>
            </div>
            `
            });

        }
        localStorage.setItem('cart', JSON.stringify(data))
    }
    shopping()
    let all = $('.allDel>input')
    all[0].onclick = function () {
        let type = all[0].checked
        console.log(type);
        data.forEach(item => {
            item.is_select = type
        })
        shopping()
        number()

    }

     function number(){
        let num = $('.sp')[0]
        // let num = $('.sp')[0]
        
        // console.log(val)
        num.onclick = (e)=>{
            // console.log(data)
            let target = e.target || e.srcElement
            if(target.className == 'add'){
                // console.log(data)
                let id = target.dataset.id - 0
                
                let goods = data.find(item=>{return item.goods_id == id})
                // console.log(goods)
                //注意点：这个地方工作中跟进商品库存决定，不能固定现在主要是为了方便调试代码
                if(goods.cart_number==10){
                    alert('亲，只能购买这么多哦!')
                    return false
                }
                goods.cart_number++
                // this.render()
                // this.total()
                shopping()
                total()
            }
             //单选功能
             if(target.className == 'select'){
                let id = target.dataset.id - 0
                console.log(1)
                //找到数组里面对应的数据修改数据的状态
                let goods = data.find(item=>{return item.goods_id == id})
                goods.is_select = !goods.is_select
                shopping()
                total()
            }
            // 全选功能
            if(target.className == 'selectAll'){
                let type = target.checked
                data.forEach(item=>{
                    item.is_select = type
                })
                shopping()
                total()
            }
            if(target.className == 'del'){
                let id = target.dataset.id - 0
                let goods = data.find(item=>{return item.goods_id == id})
                //注意点：这个地方工作中跟进商品库存决定，不能固定现在主要是为了方便调试代码
                if(goods.cart_number==1){
                    return false
                }
                goods.cart_number--
               shopping()
               total()
            }
            // 删除功能
            if(target.className == 'dell'){
                let id = target.dataset.id - 0
                let index = data.findIndex(item=>{return item.goods_id == id})
                data.splice(index, 1)
                shopping()
                total()
                
            }
           
        }
        function total(){
            let num = 0
            let price = 0
            let selectAll = $('.selectAll')[0]
            // console.log(selectAll)
            let money = $('.pay>span')[2]
            let moneys = $('.pay>span')[1]
            // console.log(money)
            let totalNum = $('.pay>span')[0]
            data.forEach(item=>{
                //总价和总的数量必须是选中状态下才计算
                if(item.is_select == true){
                    num += item.cart_number
                    price += item.cart_number * item.price
                    let res = data.every(item=>{return item.is_select == true})
                    if(res){
                        selectAll.checked = true
                    }else{
                        selectAll.checked = false
                    }
                }
            })
           totalNum.innerHTML = num
           money.innerHTML = '￥'+ price
          
           moneys.innerHTML = '总额：￥'+ price
        }
        total()
    }
number()


